package com.scs.application.modules.ext.service.impl;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.modules.ext.entity.HospitalSharePart;
import com.scs.application.modules.ext.mapper.HospitalSharePartMapper;
import com.scs.application.modules.ext.service.HospitalSharePartService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.scs.application.core.service.BaseServiceImpl;

import java.util.List;

/**
 *  医院配件分享 服务实现类
 */
@Service
public class HospitalSharePartServiceImpl extends BaseServiceImpl<HospitalSharePartMapper, HospitalSharePart> implements HospitalSharePartService {
    @Override
    @Async
    public String receiveData(List<HospitalSharePart> pushList) {
        String hpId = UserUtils.getHospital().getId();
        String hpName = UserUtils.getHospital().getName();
        synchronized (hpId.intern()){
            this.remove(Wrappers.<HospitalSharePart>query().eq("hospital_id",hpId ));
            if (ObjectUtil.length(pushList) == 0) {
                return StrUtil.format("已清空配件分享记录，医院【{}】",hpName);
            }
            for(HospitalSharePart item: pushList) {
                item.setHospitalId(hpId).setHospitalName(hpName);
            }
            this.saveBatch(pushList);

            return StrUtil.format("新增配件分享记录【{}】，医院【{}】",pushList.size(),hpName);
        }
    }
}